package com.ec.edu.server.hello.handler.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.ec.edu.common.constants.NettyConstant;
import com.ec.edu.common.constants.NettyMap;
import com.ec.edu.common.enums.MsgType;
import com.ec.edu.common.enums.RoomType;
import com.ec.edu.common.vo.ChatTextParamsVo;
import com.ec.edu.common.vo.MessageVo;
import com.ec.edu.server.hello.handler.RoomTypeHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelId;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 私聊
 */
@Slf4j
@Service("oneRoomType")
public class OneRoomTypeHandler implements RoomTypeHandler {
    @Override
    public void dealRoom(ChannelHandlerContext ctx, MsgType msgType, RoomType roomType, String jsonStr, String code) {
        switch (msgType) {
            //发送文字
            case TEXT:
                log.error("发送文字：{}", jsonStr);
                MessageVo<ChatTextParamsVo> chatTextParamsVoMessageVo = JSON.parseObject(jsonStr, new TypeReference<MessageVo<ChatTextParamsVo>>(){});
                ChatTextParamsVo chatTextParamsVo = (ChatTextParamsVo) chatTextParamsVoMessageVo.getData();
                //把聊天记录保存到数据库，同时标记消息的签收状态[未签收]

                //转发处理逻辑
                if (StringUtils.isEmpty(chatTextParamsVoMessageVo.getToClientId())) {
                    log.info("toClientId 为空，无需继续转发");
                    return;
                }
                ChannelId id = NettyMap.channelMap.get(chatTextParamsVoMessageVo.getToClientId());
                if (null == id) {
                    //TODO 写入离线消息
                    log.info("toClientId ChannelId 为空，无需继续转发");
                    return;
                }
                SocketChannel socketChannel = (SocketChannel) NettyMap.channels.find(id);
                if (socketChannel != null && socketChannel.isActive()) {
                    MessageVo messageVo = new MessageVo(msgType.getMsgType(), chatTextParamsVoMessageVo.getClientId(), chatTextParamsVoMessageVo.getToClientId(), chatTextParamsVo);
                    log.info("writeAndFlush to toClientId: {}", JSONObject.toJSONString(messageVo));
                    if (NettyConstant.MsgPac_Code.equals(code)) {
                        socketChannel.writeAndFlush(messageVo);
                    } else {
                        socketChannel.writeAndFlush(new TextWebSocketFrame(JSONObject.toJSONString(messageVo)));
                    }
                } else {
                    //TODO 写入离线消息
                    log.info("toClientId socketChannel 为空或失效，无需继续转发");
                }
                break;
            case IMAGE:
                //发送图片
                log.error("发送图片：{}", jsonStr);
                break;
            case VOICE:
                //发送语音
                log.error("发送语音：{}", jsonStr);
                break;
            case VIDEO:
                //发送视频
                log.error("发送视频：{}", jsonStr);
                break;
            case VOICE_CALL:
                //语音通话
                log.error("语音通话：{}", jsonStr);
                break;
            case VIDEO_CALL:
                //视频通话
                log.error("视频通话：{}", jsonStr);
                break;
            case SEND_LOCATION:
                //地理位置
                log.error("地理位置：{}", jsonStr);
                break;
            case SIGNED:
                //消息签收
                log.error("地理位置：{}", jsonStr);
                break;
            default:
                log.error("不能处理的消息：{}", jsonStr);
        }
    }
}
